# Check requisite packages are installed.
packages <- c(
"plotly",
"dplyr",
"RMTRCode2"
)
for (pkg in packages) {
library(pkg, character.only = TRUE)
}
# Reserved Names
candidateData <- NULL
islandInteractionsOneEmptyTwoWhich <- NULL
islandInteractionsOneTwo <- NULL
islandInteractionsOneTwoWhich <- NULL
mats <- NULL
paramFrame <- NULL
plotScalingData <- NULL
pools <- NULL
ellipsisApply <- function(..., FUN) {
lapply(as.list(...), FUN)
}
load("LM1996-NumPoolCom-QDat-2021-05.RData")
# Stop if not all are not null
stopifnot(all(unlist(ellipsisApply(
FUN = function(bool) {!is.null(bool)},
candidateData,
islandInteractionsOneEmptyTwo,
islandInteractionsOneEmptyTwoWhich,
islandInteractionsOneTwo,
islandInteractionsOneTwoWhich,
mats,
paramFrame,
plotScalingData,
pools
))))
plotScaling <- plotly::plot_ly(
plotScalingData,
x = ~Basals,
y = ~Consumers,
z = ~CommunitySize,
color = ~Dataset,
colors = c("red", "blue", "black")
)
plotScaling <- plotly::add_markers(plotScaling)
plotScaling <- plotly::layout(
plotScaling,
scene = list(
xaxis = list(type = "log"),
yaxis = list(type = "log"),
camera = list(
eye = list(
x = -1.25, y = -1.25, z = .05
)
)
)
)
plotScaling
# Check that the Two island and Three island scenarios are set-up the same.
stopifnot(unlist(lapply(islandInteractionsOneTwo, length)) ==
unlist(lapply(islandInteractionsOneEmptyTwo, length)))
stopifnot(names(islandInteractionsOneTwo) ==
names(islandInteractionsOneEmptyTwo))
# Check that the Which versions correspond correctly.
stopifnot(
unlist(lapply(islandInteractionsOneTwoWhich, function(x) {
length(RMTRCode2::CsvRowSplit(x))
}))
== unlist(lapply(islandInteractionsOneTwo, function(x) {
# We're like onions; we have LAYERS!
lapply(x, function(y) {
lapply(y, function(z) {
sum(z > 1E-6) # How many "large" entries are there?
})})}))
)
stopifnot(
unlist(lapply(islandInteractionsOneEmptyTwoWhich, function(x) {
length(RMTRCode2::CsvRowSplit(x))
}))
== unlist(lapply(islandInteractionsOneEmptyTwo, function(x) {
# We're like onions; we have LAYERS!
lapply(x, function(y) {
lapply(y, function(z) {
sum(z > 1E-6) # How many "large" entries are there?
})})}))
)
# Hybrids
# Create a count of how many times each entry will be repeated.
allCommunitiesRepeater <- 5 * unlist(lapply(islandInteractionsOneTwo, length))
# Create template.
allCommunities <- data.frame(
CombnNum = rep(0, sum(allCommunitiesRepeater)), # Should repeat all rows.
Basals = 0,
Consumers = 0,
Dataset = "",
DatasetID = 0,
Communities = "",
CommunitySize = 0,
OtherSteadyStates = 0, # To be recalculated
CommunityAbund = "",
CommunityProd = 0,
TotalID = "",
# Additional Column!, 1 for direct assembly, 0 unused.
IslandsUsed = rep(c(2,2,3,3,3), sum(allCommunitiesRepeater)/5)
)
# Retrieve the rows used to make hybrids
allCommunitiesProspects <- candidateData %>% dplyr::group_by(
CombnNum, Basals, Consumers, Dataset, DatasetID, TotalID
) %>% dplyr::select(
CombnNum:DatasetID, TotalID
) %>% dplyr::summarise(
Count = dplyr::n(), .groups = "keep"
) %>% dplyr::filter(
Count > 1
) %>% dplyr::select(
-Count
) %>% dplyr::arrange(
DatasetID, CombnNum
)
# Make sure that the names match.
stopifnot(allCommunitiesProspects$TotalID == names(allCommunitiesRepeater))
# Insert repetitions.
allCommunities$CombnNum <- rep(allCommunitiesProspects$CombnNum, allCommunitiesRepeater)
allCommunities$Basals <- rep(allCommunitiesProspects$Basals, allCommunitiesRepeater)
allCommunities$Consumers <- rep(allCommunitiesProspects$Consumers, allCommunitiesRepeater)
allCommunities$Dataset <- rep(allCommunitiesProspects$Dataset, allCommunitiesRepeater)
allCommunities$DatasetID <- rep(allCommunitiesProspects$DatasetID, allCommunitiesRepeater)
allCommunities$TotalID <- rep(allCommunitiesProspects$TotalID, allCommunitiesRepeater)
# To move over from the data.
# Communities = "",
# CommunityAbund = ""
allCommunities[allCommunities$IslandsUsed == 2, ]$Communities <-
islandInteractionsOneTwoWhich
allCommunities[allCommunities$IslandsUsed == 3, ]$Communities <-
islandInteractionsOneEmptyTwoWhich
allCommunities[allCommunities$IslandsUsed == 2, ]$CommunityAbund <-
# We're like onions; we have LAYERS!
unlist(lapply(islandInteractionsOneTwo, function(x) {
lapply(x, function(y) {
lapply(y, function(z) {
toString(z[z > 1E-6])
})
})
}))
allCommunities[allCommunities$IslandsUsed == 3, ]$CommunityAbund <-
unlist(lapply(islandInteractionsOneEmptyTwo, function(x) {
lapply(x, function(y) {
lapply(y, function(z) {
toString(z[z > 1E-6])
})
})
}))
# To calculate from the data.
# CommunitySize = 0, # To be calculated from Communities.
# OtherSteadyStates = 0, # To be recalculated last after filtering
# CommunityProd = 0, # To be recalculated after Abund stored.
allCommunities$CommunitySize <- unlist(lapply(
allCommunities$Communities, function(x) {
length(RMTRCode2::CsvRowSplit(x))
}))
for (r in 1:nrow(allCommunities)) {
allCommunities$CommunityProd[r] <- with(
allCommunities[r, ],
RMTRCode2::Productivity(
Pool = pools[[DatasetID]][[CombnNum]],
InteractionMatrix = mats[[DatasetID]][[CombnNum]],
Community = Communities,
Populations = CommunityAbund
)
)
}
# Original systems
allCommunities <- rbind(
candidateData %>% dplyr::select(
-CommunityFreq, -CommunitySeq
) %>% dplyr::mutate(
IslandsUsed = 1
),
allCommunities
)
# Treating the Productivity like one might treat a hash,
# if two rows with the same properties are assigned the same hash,
# we only keep one.
# One decimal place might be excessive,
# but we can reflect on that if results down the line are not interesting.
# For the record though, it appears that it is a decently good approach at
# removing effectively numerical duplicates.
# Not bothering, sort of, with IslandsUsed, since many times a community is
# reproduced on varying numbers of islands.
# allCommunities <- allCommunities %>% dplyr::mutate(
# tempProd = round(CommunityProd, 2)
# ) %>% dplyr::distinct(
# CombnNum, Basals, Consumers, Dataset, DatasetID, TotalID,
# Communities, CommunitySize, tempProd, IslandsUsed,
# .keep_all = TRUE
# ) %>% dplyr::select(
# -tempProd
# )
allCommunities <- allCommunities %>% dplyr::mutate(
tempProd = round(CommunityProd, 2)
) %>% dplyr::group_by(
CombnNum, Basals, Consumers, Dataset, DatasetID, TotalID,
Communities, CommunitySize, tempProd,
) %>% dplyr::summarise(
CommunityAbund = dplyr::first(CommunityAbund),
CommunityProd = dplyr::first(CommunityProd),
IslandsUsed = toString(unique(IslandsUsed)),
.groups = "drop"
) %>% dplyr::select(
-tempProd
) %>% dplyr::group_by(
CombnNum, Basals, Consumers, Dataset, DatasetID, TotalID
) %>% dplyr::mutate(
OtherSteadyStates = dplyr::n() - 1,
)
The idea is straightforward: after allowing interactions between islands, for islands that are not the same as one of the original communities, isolate the island and check to see what happens.
Looking at a longer time scale, what happens if/when invasions resume? Do the hybrid communities that emerged retain the uninvadability of the parent communities?
We check to see what happens when we treat each community as a pool for the other and perform assembly. Are the results the same as the diffusion system?
Here, we check to see if the networks created by each community (hybrid or otherwise) has mutualism embedded in it.